﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Net;
using System.Web;
using System.Web.UI.WebControls;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.UI.Master
{
    public partial class Master : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bincontents();
                ChatyahooSkype();
                BindProductNew();

                if (Session[BONSAIConstants.SHOPPING_CART] != null)
                {
                    var cart = new ShoppingCart();
                    cart.gCart = (ArrayList)Session[BONSAIConstants.SHOPPING_CART];

                    lblShoppingCart.Text = cart.gCart.Count.ToString();
                }
            }
        }
        public void Bincontents()
        {
            var lst = Application[BONSAIConstants.LISTALLTYPE] as IList<TypeProductsDTO>;

            if (lst == null) return;
            Repeater1.DataSource = lst;
            Repeater1.DataBind();

            //DropDownList1
            
            for (int i = 0; i < lst.Count; i++)
            {
                drptype.Items.Insert(i, new ListItem(lst[i].NameFull, lst[i].TypeProductsID));
            }
            drptype.Items.Insert(0, new ListItem("-- Vui lòng chọn loại --", "0"));

        }
        /// <summary>
        /// check skype is online or offline
        /// true is online
        /// false is offline
        /// </summary>
        /// <param name="str">name of skype</param>
        /// <returns></returns>
        private static bool CheckOnline(string str)
        {
            try
            {
                // Create the request.
                var url = "http://mystatus.skype.com/" + str;
                var status = false;

                var request = WebRequest.Create(url);

                // Set the method to HEAD.
                request.Method = "HEAD";

                // Get the response.
                using (var response = request.GetResponse())
                {
                    // Get the length.
                    var length = response.ContentLength;

                    if (length == 1808)
                    {
                        status = true;
                    }
                }
                return status;
            }
            catch (Exception)
            {
                return true;
            }
        }
        /// <summary>
        /// check yahoo online
        /// true is online is offline
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        private static bool Checkyahoo(string strName)
        {
            try
            {
                var client = new WebClient();
                var htmlCode = client.DownloadString("http://opi.yahoo.com/online?u=" + strName + "&m=a&t=1");
                return htmlCode != "00";
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void ChatyahooSkype()
        {
            var myPopup = new DataSet();
            myPopup.ReadXml(HttpContext.Current.Server.MapPath("~/xml/ChatOnline.xml"));
            var myDataTable = myPopup.Tables[0];
            
            var str = string.Empty;
            string path = "http://hoabonsai.com/Images/";
            for (var i = 0; i < myDataTable.Rows.Count; i++)
            {
                //string aaaaa = myDataTable.Rows[i][0].ToString();
                if (myDataTable.Rows[i][0].Equals(BONSAIConstants.YAHOO))
                {
                    if (Checkyahoo(myDataTable.Rows[i][1].ToString()))
                    {
                        str += "<a href='ymsgr:sendIM?" + myDataTable.Rows[i][1] +
                               "' ><img src='" + path + "yahoo-online.png' alt='' /></a>";

                    }
                    else
                    {
                        str += "<a href='ymsgr:sendIM?" + myDataTable.Rows[i][1] +
                               "' ><img src='" + path + "yahoo-offline.png' alt='' /></a>";
                    }
                }
                else
                {
                    if (CheckOnline(myDataTable.Rows[i][1].ToString()))
                    {
                        str += "<a href=\"skype:" + myDataTable.Rows[i][1] + "?call\" \">";
                        str += "<img src='" + path + "skype-online.png' alt='' />";
                        str += "</a>";

                    }
                    else
                    {
                        str += "<a href=\"skype:" + myDataTable.Rows[i][1] + "?call\" \">";
                        str += "<img src='" + path + "skype-offline.png' alt='' />";
                        str += "</a>";
                    }
                }

            }
            ltrYahoo.Text = str;
        }
        public void BindProductNew()
        {
            var lst = Application[BONSAIConstants.LISTPRODUCTSNEW] as IList<ProductsDTO>;
            if (lst != null)
            {
                Repeater2.DataSource = lst;
                Repeater2.DataBind();
            }
        }

        protected void ibtnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (drptype.SelectedValue.Equals("0"))
            {
                drptype.Focus();
            }
            else
            {
                Response.Redirect("~/search/" + drptype.SelectedValue + "/" + txtSearch.Text);
            }

        }

        
    }
}

   